//
//  GoodRecommedCell.m
//  danpin
//
//  Created by chuangjia on 26/8/15.
//  Copyright (c) 2015 chuangjia. All rights reserved.
//

#import "GoodRecommedCell.h"

@interface GoodRecommedCell()
@end
@implementation GoodRecommedCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor=[UIColor clearColor];
        
    }
    return self;
}
-(void)dealloc
{
    self.data=nil;
    self.key=nil;
    self.indexPath=nil;
    //    DLog(@"%s", __PRETTY_FUNCTION__);
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
}
/// 根据数据模型来显示内容
- (void)showInfo:(id)model indexPath:(NSIndexPath *)indexPath
{
    NSMutableDictionary *dict = model;
    self.data =model;
    self.indexPath =indexPath;
  
    //三列
    int totalloc=3;
    CGFloat appvieww=(DEVICE_WIDTH - PHOTO_FRAME_WIDTH*4)/3;
    CGFloat appviewh=(DEVICE_WIDTH - PHOTO_FRAME_WIDTH*4)/3;
    CGFloat margin=((DEVICE_WIDTH)-totalloc*appvieww)/(totalloc+1);
    NSMutableArray *numArray = [NSMutableArray arrayWithArray:[dict allKeys]];
    if ([numArray containsObject:@"99999999"]) {
        [numArray removeObject:@"99999999"];
    }
    int count= (int)numArray.count;
    if (count>=6) {
        count=6;
    }
    for (int i=0; i<count; i++) {
        int row=i/totalloc;//行号
        //1/3=0,2/3=0,3/3=1;
        int loc=i%totalloc;//列号
        CGFloat appviewx=margin+(margin+appvieww)*loc;
        CGFloat appviewy=margin+(margin+appviewh)*row;
        NSString *key = [numArray objectAtIndex:i];
        NSDictionary *dicdata =[dict objectForKey:key];
        UIView *temView =[self cellViewWithFrame:CGRectMake(appviewx, appviewy, appvieww, appviewh) data:dicdata index:i];
        [self addSubview:temView];
        temView=nil;
     }
    [self layoutSubviews];
}

/// 返回Cell高度
+ (CGFloat)returnCellHeight:(id)model
{
    NSMutableDictionary *dict = model;
    NSMutableArray *numArray = [NSMutableArray arrayWithArray:[dict allKeys]];
    if ([numArray containsObject:@"99999999"]) {
        [numArray removeObject:@"99999999"];
    }
    int count = (int)numArray.count;
    if (count>=6) {
        count=6;
    }
    CGFloat appvieww=(DEVICE_WIDTH - PHOTO_FRAME_WIDTH*4)/3;
    int column = 1;
    if (count>3) {
        column =2;
    }
    return appvieww*column+PHOTO_FRAME_WIDTH*(column+1);
}
-(void)setCallBack:(void(^)(BEventType,UIView*,id,id,NSIndexPath *))callback
{
    self.actionBlock=callback;
}

-(void)click:(id)sender
{
    UIButton *gest =(UIButton*)sender;
    NSMutableArray *numArray = [NSMutableArray arrayWithArray:[self.data allKeys]];
    NSString *key = [numArray objectAtIndex:gest.tag];
    NSDictionary *dicdata =[self.data objectForKey:key];
    if (self.actionBlock) {
        self.actionBlock(BEventType_good,self,dicdata,nil,self.indexPath);
    }
}
-(UIView *)cellViewWithFrame:(CGRect)frame data:(NSDictionary*)data index:(int)index
{
    UIView *cellView =[[UIView alloc] initWithFrame:frame];
    cellView.backgroundColor=[UIColor clearColor];
    CGFloat appvieww=(DEVICE_WIDTH - PHOTO_FRAME_WIDTH*4)/3;
    //创建uiview控件中的子视图
    UIImageView *appimageview=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, appvieww, appvieww)];
    
    [appimageview setContentMode:UIViewContentModeScaleAspectFit];
    NSString *iconUrl =[[data objectForKey:@"largePicURL"] replaceNullString];
    [appimageview sd_setImageWithURL:[NSURL URLWithString:iconUrl] placeholderImage:[UIImage imageNamed:@"image.png"]];
    [cellView addSubview:appimageview];
    appimageview =nil;
    //创建按钮
    UIButton *appbtn=[UIButton buttonWithType:UIButtonTypeCustom];
    appbtn.frame= CGRectMake(0, 0, appvieww, appvieww);
    appbtn.tag = index;
    [appbtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [cellView addSubview:appbtn];
    appbtn=nil;
    return cellView;
}
@end
